package com.tusuo.stds.ui.school;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_area)
public class AreaActivity extends BaseToolbarActivity<ProvinceView, ProvincePresenter>
                                implements ProvinceView {

    @ViewById(R.id.list_area_tv_name)
    TextView tvName;

    @ViewById(R.id.list_area)
    ListView listView;

    @StringRes(R.string.toolbar_title_set_area)
    String toolbarTitle;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    private UserInfo mUserInfo;
    private AreaList mAreaList;
    private int mLevel;
    private String mProvinceName, mCityName, mAreaName;
    private AreaListAdapter mCityAdapter;

    @AfterInject
    void init() {
        mUserInfo = Parcels.unwrap(getIntent().getParcelableExtra(UserInfo.EXTRA_NAME));
        mAreaList = Parcels.unwrap(getIntent().getParcelableExtra(AreaList.EXTRA_NAME));
    }

    @AfterViews
    void initData() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        mProvinceName = getIntent().getStringExtra("name");
        mCityName = "";
        mAreaName = "";
        mCityAdapter = null;

        tvName.setText(mProvinceName);

        setArea(mAreaList.Data, 2);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mLevel > 2) {
            listView.setAdapter(mCityAdapter);
            mLevel--;
        } else {
            super.onBackPressed();
        }
    }

    @NonNull
    @Override
    public ProvincePresenter createPresenter() {
        return new ProvincePresenter(this, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void showLocation(AreaList.Area area, String location) {
    }

    @Override
    public void setListViewAdapter(AreaListAdapter adapter) {}

    @Override
    public void setArea(List<AreaList.Area> area) {
    }

    @Override
    public void setArea(List<AreaList.Area> area, int level) {
        mLevel = level;
        switch (level) {
            case 2:
                mCityAdapter = new AreaListAdapter(this, area, mUserInfo, level);
                listView.setAdapter(mCityAdapter);
                break;
            default:
                listView.setAdapter(new AreaListAdapter(this, area, mUserInfo, level));
                break;
        }
    }

    @ItemClick(R.id.list_area)
    void listItemClicked(AreaList.Area area) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        if (area.Children.size() > 0) {
            setArea(area.Children, mLevel + 1);

            if (TextUtils.isEmpty(mCityName)) {
                mCityName = area.Name;

                String name = mProvinceName + area.Name;
                tvName.setText(name);
            } else if (TextUtils.isEmpty(mAreaName)) {
                mAreaName = area.Name;

                String name = mProvinceName + mCityName + area.Name;
                tvName.setText(name);
            }
        } else {
            Intent intent = new Intent();
            intent.setClass(this, SchoolActivity_.class);
            intent.putExtra(UserInfo.EXTRA_NAME, Parcels.wrap(mUserInfo));

            intent.putExtra("name", tvName.getText() + area.Name);
            intent.putExtra("areaId", area.Id);

            startActivity(intent);
        }
    }
}
